﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace QSearcher.QCrawler
{
    public partial class FrmCrawler : Form
    {
        public static FrmCrawler T { set; get; }

        Crawler crawler = new Crawler();

        public FrmCrawler()
        {
            InitializeComponent();
        }

        private void FrmCrawler_Load(object sender, EventArgs e)
        {
            btnStop.Enabled = false;

            FrmCrawler.T = this;

            crawler.Stoped += () =>
            {
                this.Invoke(new Action(() =>
                {
                    btnStop.Enabled = false;
                    gbSetting.Enabled = true;
                    btnStart.Enabled = true;
                }));
            };

            var analyzers = IAnalyzer.GetAnalyzers();
            foreach (var analyzer in analyzers)
            {
                RadioButton rb = new RadioButton();
                rb.Text = analyzer.Name;
                tt.SetToolTip(rb, analyzer.Remark);
                rb.Tag = analyzer;
                panelAnalyzer.Controls.Add(rb);
            }
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            if (crawler.IsRunning)
            {
                e.Cancel = true;
                MessageBox.Show("提示：请先停止机器人！");
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            gbSetting.Enabled = false;
            btnStop.Enabled = true;

            IAnalyzer analyzer = null;
            var analyzers = new List<Action<int>>();
            foreach (RadioButton rb in panelAnalyzer.Controls)
            {
                if (rb.Checked)
                {
                    analyzer = rb.Tag as IAnalyzer;
                    analyzers.Add(analyzer.Run);
                    break;
                }
            }
            if (analyzer != null)
            {
                string startUrl = tbUrl.Text.Trim();
                if (startUrl != "") BizLayer.Self.SendStartUrl(analyzer.Name, startUrl);
            }
            crawler.Start((uint)numRequest.Value, (uint)numAnalyze.Value, analyzers.ToArray(), (uint)numDownload.Value);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            crawler.Stop();
        }

        private void txtLog_TextChanged(object sender, EventArgs e)
        {
            if (txtLog.Text.Length > 1024) txtLog.Text = txtLog.Text.Substring(0, 1024);
        }

        private string log = "{0}_" + Guid.NewGuid().ToString() + ".log";
        public void SendMsg(string msg)
        {
            msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "\t" + msg + Environment.NewLine;
            this.Invoke(new Action(() =>
            {
                txtLog.Text = msg + txtLog.Text;
                string logFile = "logs" + Path.DirectorySeparatorChar + string.Format(log, DateTime.Now.ToString("yyyy-MM-dd"));
                Crawler.CheckDirectory(logFile);
                File.AppendAllText(logFile, msg, Encoding.Default);
            }));
        }
    }
}
